package com.zc.mychart.utils;


import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by zhangchi on 2016/6/21.
 */
public class FormatUtil {

    public static String getDataYMDFromTimestamp(long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return format.format(new Date(timestamp));
    }

    public static String getDataMDHMFromTimestamp(long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
        return format.format(new Date(timestamp));
    }

    public static String getDataHFromTimestamp(long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("HH", Locale.CHINA);
        return format.format(new Date(timestamp));
    }

    public static String getDataDHMTimestamp(long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
        return format.format(new Date(timestamp));
    }

    public static String getImageTimeStep() {
        String params = "?" + getDataDHMTimestamp(System.currentTimeMillis());
        return params;
    }

    public static String dateToStamp(String bgtime, String edtime) {
        if (bgtime == null || edtime == null || bgtime.equals("") || edtime.equals("")) return "";
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long from = Long.valueOf(bgtime) ;
        long to = Long.valueOf(edtime) ;
        long hour = (to - from) / nh;
        long minute = (to - from) % nd % nh / nm;
        double hours = (double) hour;
        double minutes = (double) minute;
        int a = (int) hours;
        int b = (int) minutes;
        return a + "时" + b + "分";
    }

    /**
     * 将秒数转换为日时分秒，
     *
     * @param second
     * @return
     */
    public static String secondToTime(long second) {
        second = second / 1000;
        long days = second / 86400; //转换天数
        second = second % 86400;//剩余秒数
        long hours = second / 3600; //转换小时
        second = second % 3600; //剩余秒数
        long minutes = second / 60;//转换分钟
        second = second % 60;//剩余秒数
        if (days > 0) {
            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
        }
        if (hours > 0) {
            return hours + "小时" + minutes + "分" + second + "秒";
        } else {
            return minutes + "分" + second + "秒";
        }
    }

    /**
     * 将秒数转换为日时分秒，
     *
     * @param second
     * @return
     */
    public static String secondToDay(long second) {
        second = second / 1000;
        long day = second / 86400;
        if (day > 0) {
            return String.valueOf(second / 86400) + "天";
        } else {
            return String.valueOf(second / 3600) + "时";
        }
    }

    public static String[] secondToTimeByHHS(long second) {
        String[] hhs = new String[3];
        second = second / 1000;
        long hours = second / 3600; //转换小时
        second = second % 3600; //剩余秒数
        long minutes = second / 60;//转换分钟
        second = second % 60;//剩余秒数
        hhs[0] = String.valueOf(hours);
        hhs[1] = minutes >= 10 ? String.valueOf(minutes) : "0" + minutes;
        hhs[2] = second >= 10 ? String.valueOf(second) : "0" + second;
        return hhs;
    }

    public static SpannableStringBuilder getStyleText(String text) {
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(Color.BLACK), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.GRAY), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.RED), text.indexOf("利", 6) + 1, text.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }

    public static String formatDoubleFloor(double value){
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("#");
        df.setRoundingMode(RoundingMode.FLOOR);
        return df.format(decimal.doubleValue());
    }


    public static String formatFollowMoney(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("####,###,###.##");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    public static String formatDouble(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("#");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    public static String formatDouble1(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("#.#");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    public static String formatDouble2(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("#.##");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    public static String formatDouble4(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("#.####");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    public static String formatMoney(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    public static String formatNumberForOne(double value) {
        BigDecimal decimal = new BigDecimal(String.valueOf(value));
        DecimalFormat df = new DecimalFormat("0.0");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(decimal.doubleValue());
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * sp转换px
     */
    public static int sp2px(Context context, int spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 根据毫秒时间戳来格式化字符串
     * 今天显示今天、昨天显示昨天、前天显示前天.
     * 早于前天的显示具体年-月-日，如2017-06-12；
     *
     * @param timeStamp 毫秒值
     * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
     */
    public static String format(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        if (timeStamp >= todayStartMillis) {
            return "今天    " + sdf.format(new Date(timeStamp));
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if (timeStamp >= yesterdayStartMilis) {
            return "昨天    " + sdf.format(new Date(timeStamp));
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if (timeStamp >= yesterdayBeforeStartMilis) {
            return "前天    " + sdf.format(new Date(timeStamp));
        }
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd    HH:mm");
        return sdfDate.format(new Date(timeStamp));
    }

    /**
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     *
     * @param long_time
     * @return
     */
    public static String getTimeStateNew(String long_time) {
        String long_by_13 = "1000000000000";
        String long_by_10 = "1000000000";
        if (Long.valueOf(long_time) / Long.valueOf(long_by_13) < 1) {
            if (Long.valueOf(long_time) / Long.valueOf(long_by_10) >= 1) {
                long_time = long_time + "000";
            }
        }
        Timestamp time = new Timestamp(Long.valueOf(long_time));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//    System.out.println("传递过来的时间:"+format.format(time));
//    System.out.println("现在的时间:"+format.format(now));
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;
//    System.out.println("天数:"+time_conver/day_conver);
        if ((time_conver / day_conver) <= 3) {
            temp_conver = time_conver / day_conver;
            if (temp_conver <= 3 && temp_conver >= 1) {
                return temp_conver + "天前";
            } else {
                temp_conver = (time_conver / hour_conver);
                if (temp_conver >= 1) {
                    return temp_conver + "小时前";
                } else {
                    temp_conver = (time_conver / min_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "分钟前";
                    } else {
                        return "刚刚";
                    }
                }
            }
        } else {
            return format.format(time);
        }
    }
}